package com.foot.travel.viewholder;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.foot.travel.R;
import com.foot.travel.activity.ShowImageActivity;
import com.foot.travel.adapter.ImageAdapter;
import com.koalac.ironman.common.adapter.ViewHolderModel;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.DeleteGood;
import com.koalac.ironman.net.pojo.request.DeleteMyGallery;
import com.koalac.ironman.net.pojo.request.GalleryClickGood;
import com.koalac.ironman.net.pojo.response.GalleryItem;
import com.koalac.ironman.net.pojo.response.GalleryPicture;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.util.DialogUtils;
import com.koalac.ironman.util.ScreenInfoUtil;
import com.koalac.ironman.util.StringUtils;
import com.koalac.ironman.util.TimeUtil;
import com.koalac.ironman.widget.ImageLoadAgreement.ImageType;
import com.koalac.ironman.widget.LifeqImageView;
import com.koalac.ironman.widget.LoadingDialog;
import com.koalac.ironman.widget.NoScrollGridView;

public class MyGalleryItemViewHolder extends ViewHolderModel<GalleryItem> {

	LifeqImageView singlePhotoView;
	NoScrollGridView imageListView;
	TextView mood;
	TextView date;
	TextView likeCount;
	TextView likeTag;
	TextView delete;

	public static final int NO_PIC = 0;
	public static final int SINGLE_PIC = 1;

	public MyGalleryItemViewHolder(Context context, List<GalleryItem> list) {
		super(context, list);
	}

	@Override
	public void initViewHoler(int position, View convertView) {
		singlePhotoView = (LifeqImageView) convertView
				.findViewById(R.id.single_photo);
		imageListView = (NoScrollGridView) convertView
				.findViewById(R.id.image_list);
		mood = (TextView) convertView.findViewById(R.id.mood);
		date = (TextView) convertView.findViewById(R.id.date);
		likeCount = (TextView) convertView.findViewById(R.id.like_count);
		likeTag = (TextView) convertView.findViewById(R.id.like_tag);
		delete = (TextView) convertView.findViewById(R.id.delete);
	}

	@Override
	public void setViewHolerValues(final int position, View convertView) {
		final GalleryItem galleryItem = list.get(position);
		final List<GalleryPicture> galleryPictureList = galleryItem
				.getGalleryPictureList();
		int pictureSize = getPictureListSize(galleryPictureList);
		switch (pictureSize) {
		case NO_PIC:
			singlePhotoView.setVisibility(View.GONE);
			imageListView.setVisibility(View.GONE);
			break;
		case SINGLE_PIC:
			singlePhotoView.setVisibility(View.VISIBLE);
			singlePhotoView.setOnClickListener(new OnClickListener() {
			    
			    @Override
			    public void onClick(View v) {
				Intent intent = new Intent(context,ShowImageActivity.class);
				ArrayList<String> imageUrls = new ArrayList<String>();
				for (GalleryPicture galleryPicture:galleryPictureList) {
				    imageUrls.add(galleryPicture.getPicurl());
				}
				intent.putExtra(ShowImageActivity.IMAGE_URLS_KEY, imageUrls);
				intent.putExtra(ShowImageActivity.IMAGE_URL_INDEX, 0);
				context.startActivity(intent);
			    }
			});
			imageListView.setVisibility(View.GONE);
			singlePhotoView.loadFile(galleryPictureList.get(0).getPicurl(),
					ImageType.SourceImage, null, null);
			break;
		default:
			singlePhotoView.setVisibility(View.GONE);
			imageListView.setVisibility(View.VISIBLE);
			int  itemSize = (ScreenInfoUtil.getScreenWidth(context) - ScreenInfoUtil.dp2px(context, 30)) / 3;
			ImageAdapter imageAdapter = new ImageAdapter(context,
					galleryPictureList,itemSize);
			imageListView.setAdapter(imageAdapter);
			imageListView.setOnItemClickListener(new OnItemClickListener() {

			    @Override
			    public void onItemClick(AdapterView<?> parent,
				    View view, int position, long id) {
				Intent intent = new Intent(context,ShowImageActivity.class);
				ArrayList<String> imageUrls = new ArrayList<String>();
				for (GalleryPicture galleryPicture:galleryPictureList) {
				    imageUrls.add(galleryPicture.getPicurl());
				}
				intent.putExtra(ShowImageActivity.IMAGE_URLS_KEY, imageUrls);
				intent.putExtra(ShowImageActivity.IMAGE_URL_INDEX, position);
				context.startActivity(intent);
			    }
			});
			break;
		}
		mood.setText(galleryItem.getMessage());
		date.setText(TimeUtil.getTimeDescription(galleryItem.getDateline()));
		likeCount.setText(galleryItem.getGoodNumber() + "人赞");
		if (galleryItem.getGood_flag() == GalleryItem.LIKED) {
		    likeTag.setText("取消");
		}else {
		    likeTag.setText("赞");
		}
		likeTag.setOnClickListener(new OnClickListener() {
		    
		    @Override
		    public void onClick(View v) {
			if (galleryItem.getGood_flag() == GalleryItem.LIKED) {
			    cancelLike(galleryItem);
			}else {
			    like(galleryItem);
			}
		    }
		});
		delete.setOnClickListener(new OnClickListener() {
		    
		    @Override
		    public void onClick(View v) {
			showDeleteDialog(position);
		    }
		});
	}
	
	
	private void like(final GalleryItem galleryItem){

		GalleryClickGood clickGood = new GalleryClickGood();
		clickGood.setId(galleryItem.getId());
		MyRequest.request(context, clickGood, new ResponseCallback() {

		    @Override
		    public void process(boolean result, Context context,
			    Response data, String msg) {
			if (result) {
			    galleryItem.setGoodNumber(galleryItem
				    .getGoodNumber() + 1);
			    likeCount.setText(galleryItem.getGoodNumber()
				    + "人赞");
			    galleryItem.setGood_flag(GalleryItem.LIKED);
			    likeTag.setText("取消");
			}
		    }
		});
	    }
	    
	    private void cancelLike(final GalleryItem galleryItem){
		DeleteGood deleteGood = new DeleteGood();
		deleteGood.setId(galleryItem.getId());
		MyRequest.request(context, deleteGood, new ResponseCallback() {
		    
		    @Override
		    public void process(boolean result, Context context, Response data,
			    String msg) {
			if (result) {
			    galleryItem.setGoodNumber(galleryItem
				    .getGoodNumber() - 1);
			    likeCount.setText(galleryItem.getGoodNumber()
				    + "人赞");
			    galleryItem.setGood_flag(GalleryItem.UNLIKE);
			    likeTag.setText("赞");
			}
		    }
		});
	    }
	private void showDeleteDialog(final int position) {
	    DialogUtils.createConfirmDialog(context, "", "确定删除该条旅图?", "确定", "取消", new DialogInterface.OnClickListener() {
	        
	        @Override
	        public void onClick(DialogInterface dialog, int which) {
	            /**
			 * 删除该动态
			 */
			final LoadingDialog loadingDialog = LoadingDialog.createLoadingDialog(context, "", "正在删除...");
			loadingDialog.show();
			DeleteMyGallery deleteMyGallery = new DeleteMyGallery();
			deleteMyGallery.setId(list.get(position).getId());
			MyRequest.request(context, deleteMyGallery, new ResponseCallback() {
			    
			    @Override
			    public void process(boolean result, Context context, Response data,
				    String msg) {
				loadingDialog.cancel();
				if (result) {
				    list.remove(position);
				    getNotifyDataSetChanged().setNotifyDataSetChanged();
				}
			    }
			});
	        }
	    }, new DialogInterface.OnClickListener() {
	        
	        @Override
	        public void onClick(DialogInterface dialog, int which) {
	    	
	        }
	    }, DialogUtils.NO_ICON, true).show();
	}
	private int getPictureListSize(List<GalleryPicture> galleryPictureList){
	    int count = 0;
	    for (GalleryPicture galleryPicture : galleryPictureList) {
		if (!StringUtils.isEmpty(galleryPicture.getPicurl())) {
		    count++;
		}
	    }
	    return count;
	}
}
